/*
 * Copyright (c) 2021 Huawei Device Co., Maker-IoT-PD.
 * Licensed under the Apache License,Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import fetch from '@system.fetch';
import router from '@ohos.router';
import prompt from '@system.prompt';
var counter = 10;
var timer = null;
var tar=1;
var cat=3;
export default {
    data: {
        types:'Bend',
        status:'ON',
        kneading_id:'8',
        seconds:'0',
        write:'100',
        progress: 10,
        vals1:"待测",
        vals2:"待测",
        nowDate:"",
        winfo:"",
        linechart:"",

        //        time:"2022-04-22 10:56:08",
        startText:"开始检测",
        lineData: [
            {
                strokeColor: '#0081ff',
                fillColor: '#cce5ff',
                data: [],
                gradient: true,
            }
        ],
        lineOps: {
            xAxis: {
//                min: -3600,
//                max: -3400,
                min:0,
                max: 20,
                axisTick:0,
                color:`#353535`,
                display: true,
            },
            yAxis: {
                min: -1,
                max:1,
                axisTick:false,
                color:`#353535`,
                display: true,
            },
            series: {
                lineStyle: {
                    width: "3px",
                    smooth: false,
                },
                headPoint: {
                    shape: "circle",
                    size: 10,
                    strokeWidth: 5,
                    fillColor: '#ffffff',
                    strokeColor: '#007aff',
                    display: true,
                },
                loop: {
                    margin: 2,
                    gradient: true,
                }
            }
        },
    },
    run(){
        counter--;
        if (tar==2&&counter!=-1) {
            this.seconds = counter.toString();
            cat=counter.toString();
            this.write=cat*10;
            if(cat==0){
                this.startText="重新测试"
                tar=3;
            }
        } else{
            this.seconds = "0";
            clearInterval(timer);
            timer = null;
            console.info(this.Account)
            console.info(JSON.stringify({"types":this.types,"status":this.status,"kneading_id":this.kneading_id}));
            fetch.fetch({
                url:`xxxx/get_specified_accel_data?date=`+this.nowDate,
                //xxxx/get_bend_data
                method: 'POST',
                headers: {
                    'User-Agent': 'Apipost client Runtime/+https://www.apipost.cn/',
                    'Content-Type': 'application/json'
                },
                responseType:"json", //请求的参数类型
                //如果获取数据成功的话执行以下函数
                success:(resp)=>
                {
                    this.winfo = JSON.parse(resp.data);  //令获取到的数据赋给winfo
                    var resp_accel = JSON.stringify(this.winfo.results.accel)
                    let dataStrArr = resp_accel.split(",")
                    dataStrArr.shift()
                    dataStrArr.pop()
                    let dataIntArr = []
                    dataStrArr.forEach(item =>{
                        dataIntArr.push(+item);
                    });
//                    prompt.showToast({
//                        message:dataIntArr,
//                        duration:3000
//                    })
                    this.lineData[0].data = dataIntArr

                    for (var i = 0; i < dataIntArr.length; i++) {
                    //数据加到图形中
                    this.$refs.linechart.append({
                        serial: 0,
                        data: [ dataIntArr[i]]
                    })
                }

                    //                        this.vals1=JSON.stringify(this.winfo.results[0].Kneading.num_kneading)
                    //                        this.bend_index_finger = JSON.stringify(this.winfo.results[0].angle.bend_index_finger);
                    //                        this.bend_middle_finger = JSON.stringify(this.winfo.results[0].angle.bend_middle_finger);
                    this.aver_accel = JSON.stringify(this.winfo.results.aver_accel);
                    this.frequency = JSON.stringify(this.winfo.results.frequency)

                    //                        this.end_data = (this.bend_index_figner + this.bend_middle_finger + this.bend_ring_finger)/3;
                    console.info("返回的数据："+resp.data)//打印出数据
//                    prompt.showToast({
//                        message:this.aver_accel,
//                        duration:3000
//                    });
//                    this.lineData[0].data = JSON.parse(this.winfo.results.accel)
//                    this.aver_accel= this.aver_accel.replace("\"","").replace("\"","");
//                    this.this.frequency= this.this.frequency.replace("\"","").replace("\"","");
                    this.aver_accel = this.aver_accel.replace("\"","").replace("\"","");
                    var aver_accel_float = parseFloat(this.aver_accel).toFixed(2);
//                    this.vals1=this.aver_accel.replace("\"","").replace("\"","");
                    this.vals1 = aver_accel_float;
                    this.vals2 = this.frequency.replace("\"","").replace("\"","");
                },
                //如果获取数据失败则执行以下函数
                fail:(resp)=>
                {
                    this.winfo = resp.data;
                    console.info("获取数据失败："+this.winfo)
                }
            });
            tar=1;
            counter = 10;
        }
    },
    launch3() {
        //发起网络请
        if(tar==1){
            tar=2;
            var date1 = new Date();
            var nowMonth = date1.getMonth() + 1;
            var strDate = date1.getDate();
            // 添加分隔符“-”
            var seperator = "-";

            // 对月份进行处理，1-9月在前面添加一个“0”
            if (nowMonth >= 1 && nowMonth <= 9) {
                nowMonth = "0" + nowMonth;
            }

            // 对月份进行处理，1-9号在前面添加一个“0”
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }

            // 最后拼接字符串，得到一个格式为(yyyy-MM-dd)的日期
            this.nowDate = nowMonth + seperator + strDate;
            console.info(this.nowDate);
            // 获取当前是几号
            clearInterval(timer);
            this.seconds = counter.toString();
            timer = setInterval(this.run, 1000);
            this.startText="检测中..."
            console.info(this.Account)
            console.info(JSON.stringify({"types":this.types,"status":this.status,"kneading_id":this.kneading_id}));
            fetch.fetch({
                url:`xxxx/get_command?types=`+this.types+'&status='+this.status,//资源地址
                //设置header伪装
                method: 'POST',
                headers: {
                    'User-Agent': 'Apipost client Runtime/+https://www.apipost.cn/',
                    'Content-Type': 'application/json'
                },
                responseType:"json", //请求的参数类型
                //如果获取数据成功的话执行以下函数
                success:(resp)=>
                {
                    this.winfo = resp.data;  //令获取到的数据赋给winfo
                    console.info("返回的数据："+this.winfo)//打印出数据
                },
                //如果获取数据失败则执行以下函数
                fail:(resp)=>
                {
                    this.winfo = resp.data;
                    console.info("获取数据失败："+this.winfo)
                }
            });
        }else if(tar==3){
            this.startText="开始检测"
            tar=1;
        }
        else{
            this.startText="重新检测"
        }
    },
    change: function(e) {
        console.log("Tab index: " + e.index);
    },
    launch1() {
        router.push ({
            url: 'pages/action4/action4',
        });
    },
    launch2() {
        router.push ({
            url: 'pages/action5/action5',
        });
    },
    launch4() {
        router.push ({
            url: 'pages/niam/niam',
        });
    },
}
